import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {HttpService} from "../../service/http/http.service";
import {FormControl, FormGroup, NonNullableFormBuilder, Validators} from "@angular/forms";
import {NzMessageService} from "ng-zorro-antd/message";

@Component({
  selector: 'app-dict',
  templateUrl: './dict.component.html',
  styleUrls: ['./dict.component.scss']
})
export class DictComponent implements OnInit {

  listOfData: any[] = [];
  okLoading = false;
  validateForm: FormGroup<{
    id:FormControl<number>;
    filedName: FormControl<string>;
    isRequired:FormControl<number>;
  }>;

  constructor(private http:HttpService,
              private msg:NzMessageService,
              private cdr: ChangeDetectorRef,
              private fb: NonNullableFormBuilder) {

    this.validateForm = this.fb.group({
      id: [0],
      filedName: ['', [Validators.required]],
      isRequired: [0],
    });

  }

  ngOnInit() {
    this.getList();
  }

  getList(){

    let url = "/config/getAll"
    this.http.get(url,{}).then((res:any)=>{
      res = res.sort((item1:any,item2:any)=>{
        return (new Date(item2.gmtCreated+":00")).getTime() - (new Date(item1.gmtCreated+":00")).getTime()
      });
      this.listOfData = res;
      this.sortData();
    })
  }

  isShow = 1;

  changeShow(item:any){
    item.isShow = item.isShow == 1 ? 0 : 1;
    let url = "/config/update";
    this.http.postBody(url,item).then(res=>{})
    this.sortData();
  }

  sortData(){
    let array:any[] = [];
    let hide:any[] = [];
    let data:any[] = [];
    for (let i = 0; i < this.listOfData.length; i++) {
      if (this.listOfData[i].isShow == 1){
        array.push(this.listOfData[i]);
      }else {
        hide.push(this.listOfData[i]);
      }
    }
    array = array.sort((item1:any,item2:any)=>{
      return (new Date(item2.gmtCreated+":00")).getTime() - (new Date(item1.gmtCreated+":00")).getTime()
    });
    hide = hide.sort((item1:any,item2:any)=>{
      return (new Date(item2.gmtCreated+":00")).getTime() - (new Date(item1.gmtCreated+":00")).getTime()
    });
    data = data.concat(array);
    data = data.concat(hide);
    this.listOfData = data;
  }

  isEdit = false;
  edit(item:any){
    this.isEdit = true;
    this.isShow = item.isShow;
    this.validateForm.reset({
      id:item.id,
      filedName:item.filedName,
      isRequired:item.isRequired ? item.isRequired:0
    });
    this.isVisible = true;
  }

  isVisible = false;
  add(){
    this.isEdit = false;
    this.validateForm.reset();
    this.isVisible = true;
  }

  handleCancel(){
    this.isVisible = false;
  }

  isRequired(data:any){
    if (data.isRequired){
      if (data.isRequired == 0){
        return "否";
      }else {
        return "是"
      }
    }
    return "否";
  }

  handleOk(){
    if (this.validateForm.valid) {
      let value = this.validateForm.value;
      let url = "/config/add"
      if (this.isEdit){
        url = "/config/update"
      }
      this.okLoading = true;
      this.http.postBody(url,value).then(res=>{
        this.okLoading = false;
        this.isVisible = false;
        if (this.isEdit){
          this.msg.success("修改成功");
        }else {
          this.msg.success("添加成功");
        }
        this.getList();
      }).catch(err=>{
        this.okLoading = false;
        this.msg.error(err);
      });
    } else {
      Object.values(this.validateForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
    }
  }
}
